package com.aolong.archives.core.sysmanage.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.aolong.archives.common.resource.I18nBundleUtil;
import com.aolong.archives.common.util.Const;
import com.aolong.archives.core.sysmanage.dao.IUserDao;
import com.aolong.archives.core.sysmanage.entity.UserEntity;
import com.aolong.archives.core.sysmanage.service.ILoginService;
import com.rock.platform.exception.BaseException;
import com.rock.platform.service.BaseService;
import com.rock.platform.util.MD5;
import com.rock.platform.util.SessionUtils;

public class LoginService extends BaseService implements ILoginService {

	private IUserDao daoUser;
	
	public Boolean login(HttpServletRequest request) {
		
		String loginName = ServletActionContext.getRequest().getParameter("loginname");
		String password = ServletActionContext.getRequest().getParameter("password");
		String language = ServletActionContext.getRequest().getParameter("language");
		
		UserEntity entity = this.LoginCheck(loginName, password);
		if (entity != null)
		{
			this.setSession(entity,request,language);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public Boolean logoff()
	{
		return true;
	}
	
	private UserEntity LoginCheck(String loginName, String password)
	{
		UserEntity entity = this.daoUser.findByLoginName(loginName);
		
		if (entity == null)
		{   
			throw new BaseException("["+loginName+"] "+I18nBundleUtil.getI18n("Invalid Username"));
		}
		else
		{
			if (!Const.Status.NORMAL.toValue().equals(entity.getStatus()))
			{
				throw new BaseException("["+loginName+"] "+I18nBundleUtil.getI18n("User is not activated"));
			}
			if (!MD5.verifyPassword(password, entity.getPassword()))
			{
				throw new BaseException("["+loginName+"] "+I18nBundleUtil.getI18n("Invalid password"));
			}
		}
		return entity;
	}
	
	private void setSession(UserEntity entity,HttpServletRequest request,String language)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = sdf.format(new Date());
		
		SessionUtils.setAttribute(SessionUtils.SKEY_LOGINNAME, entity.getLoginName());
		SessionUtils.setAttribute(SessionUtils.SKEY_USERID, entity.getUserId());
		SessionUtils.setAttribute(SessionUtils.SKEY_USERCODE, entity.getUserCode());
		SessionUtils.setAttribute(SessionUtils.SKEY_USERNAME, entity.getUserName());
		SessionUtils.setAttribute(SessionUtils.SKEY_USERAUTH, "");
		SessionUtils.setAttribute(SessionUtils.SKEY_LOGINTIME, strDate);
		SessionUtils.setAttribute(SessionUtils.SKEY_LANGUAGE, language);
		SessionUtils.setAttribute(SessionUtils.SKEY_REMOTEHOST, request.getRemoteHost());
		SessionUtils.setAttribute(SessionUtils.SKEY_REMOTEIP, request.getRemoteAddr());
		SessionUtils.setAttribute(SessionUtils.SKEY_TOKENID, request.getCookies()[0].getValue());
	}
	
	public IUserDao getDaoUser() {
		return daoUser;
	}

	public void setDaoUser(IUserDao daoUser) {
		this.daoUser = daoUser;
	}
}
